####  Figure S16: Random forest model performance (all predictors) ####

# Libraries
# library(tidyverse)
# library(tidymodels)
# library(rio)
# library(here)
# library(patchwork)
# library(ggthemes)

# source(here("Code","Functions","funcs_theme.R"))

rf_fit_mods = import(here("Output","randforest_mods.rds"))

pred_vars = rep(c("norm_judgesre","norm_loyaltyre","norm_pollingre",
                  "norm_executivere","norm_censorshipre", paste0("violence",3:6,"re")),each = 3)

rf_metrics = imap_dfr(rf_fit_mods, \(x, idx){
  x |> 
    collect_metrics() |> 
    mutate(id = pred_vars[idx])
})

rf_metrics$ModelSample = rep(c("Pooled","Democrats", "Republicans"), each = 2, 9)

df_metrics = rf_metrics |>
  filter(!(id %in% c("violence1re","violence2re"))) |> 
  mutate(typ = ifelse(str_detect(id, "norm"), "Democratic Norms", "Violence")) |> 
  mutate(
    .metric = case_match(.metric,
                         "accuracy" ~ "Accuracy",
                         "roc_auc" ~ "ROC AUC"
    ),
    id = case_match(id,
                    "norm_pollingre" ~ "Reduce outparty\npolling stations",
                    "norm_loyaltyre" ~ "More loyal to\nparty than election\nrules and constitution",
                    "norm_judgesre" ~ "Ignore outparty\ncourt decisions",
                    "norm_executivere" ~ "President should\ncircumvent congress",
                    "norm_censorshipre" ~ "Censor partisan\nmedia",
                    "violence6re" ~ "Murder",
                    "violence5re" ~ "Assault with\ndeadly weapon",
                    "violence4re" ~ "Arson",
                    "violence3re" ~ "Assault"#,
                    #"violence2re" ~ "Vandalism",
                    #"violence1re" ~ "Protest without\npermit"
    )
  )

p_metrics_norms = df_metrics |> 
  filter(typ == "Democratic Norms") |> 
  ggplot(aes(x = .estimate,
             y = tidytext::reorder_within(id, .estimate, .metric),
             label = paste0(round(.estimate, 3)*100,"%"),
             fill = ModelSample)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(x = 0.05), color = "white",
            hjust = 0,
            position = position_dodge(0.9),
            size = 3) +
  tidytext::scale_y_reordered() +
  facet_wrap(~ .metric, scales = "free_y") +
  scale_x_continuous(labels = scales::percent,
                     limits = c(0,1)) +
  scale_fill_manual(name = "Party", values = c("blue","grey25","red")) +
  labs(y = "Democratic Norms",
       x = NULL) +
  theme_prl() +
  theme(legend.position = "none")

p_metrics_violence = df_metrics |> 
  filter(typ == "Violence") |> 
  ggplot(aes(x = .estimate,
             y = tidytext::reorder_within(id, .estimate, .metric),
             label = paste0(round(.estimate, 3)*100,"%"),
             fill = ModelSample)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(x = 0.05), color = "white",
            hjust = 0,
            position = position_dodge(0.9),
            size = 3) +
  tidytext::scale_y_reordered() +
  facet_wrap(~ .metric, scales = "free_y") +
  scale_x_continuous(labels = scales::percent,
                     limits = c(0,1)) +
  scale_fill_manual(name = "Party", values = c("blue","grey25","red")) +
  labs(y = "Violence",
       x = "Estimate") +
  theme_prl()

fig_s16 = p_metrics_norms / p_metrics_violence

print(fig_s16)

ggsave(here("Plots","Supplementary","figure_s16.png"),
       fig_s16,
       dpi = 600,
       units = "in",
       width = 8, height = 7)